package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.dto.PageQueryDTO;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
@Slf4j
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    /**
     * 分页查询服务人员实名认证审核列表
     *
     * @param queryDTO 服务人员认证信息
     * @return 分页结果
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO> pageQueryWorkerCertificationAudit(
            WorkerCertificationAuditPageQueryReqDTO queryDTO
    ) {
        // 解析分页参数
        Page<WorkerCertification> page = PageUtils.parsePageQuery(queryDTO, WorkerCertification.class);
        
        // 构建查询条件
        LambdaQueryWrapper<WorkerCertification> queryWrapper = Wrappers.<WorkerCertification>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(queryDTO.getName()), WorkerCertification::getName, queryDTO.getName())
                .eq(ObjectUtil.isNotEmpty(queryDTO.getIdCardNo()), WorkerCertification::getIdCardNo, queryDTO.getIdCardNo())
                .orderByDesc(WorkerCertification::getCreateTime);
        
        // 执行分页查询
        Page<WorkerCertification> certificationPage = baseMapper.selectPage(page, queryWrapper);
        String jsonStr = JSONUtil.toJsonStr(certificationPage);
        log.info("分页查询服务人员实名认证审核列表结果：{}", jsonStr);

        // 转换为响应DTO并返回
        return PageUtils.toPage(certificationPage, WorkerCertificationAuditResDTO.class);
    }
    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    @Override
    public void add(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        WorkerCertification workerCertification = BeanUtil.copyProperties(workerCertificationUpdateDTO, WorkerCertification.class);
        workerCertification.setCertificationStatus(workerCertificationUpdateDTO.getCertificationStatus());
        baseMapper.insert(workerCertification);
    }
}
